package com.xizi.lock;

import java.util.concurrent.locks.*;

public class ReentrantDemo {

    private Lock lock = new ReentrantLock();

    public void demo() {
        //获取锁
        lock.lock();
        System.out.println("begin demo");
        //执行demo2方法
        demo2();
        lock.unlock();
    }

    public void demo2() {
        //获取锁
        lock.lock();
        System.out.println("begin demo2");
        //释放锁
        lock.unlock();
    }

    public static void main(String[] args) {
        ReentrantDemo rd = new ReentrantDemo();
        //启动线程调用demo方法
        new Thread(rd::demo).start();
    }
}
